﻿using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITesting;
using BackOffice.Context.UltiPro.BackOfficeUIMapClasses;
using System.Drawing;
using System.IO;
using System.Windows.Input;
using BackOffice.Context.UltiPro.PageObjects;
using BackOffice.Context.UltiPro.Controls;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;

namespace BackOffice.Context.UltiPro.Macros
{
    /// <summary>
    /// Common macros
    /// </summary>
    public class UIMacros
    {
        /// <summary>
        /// Gets the UI map.
        /// </summary>
        /// <value>
        /// The UI map.
        /// </value>
        private static BackOfficeUIMap UIMap
        {
            get
            {
                return new BackOfficeUIMap();
            }
        }

        /// <summary>
        /// Launches UltiPro
        /// </summary>
        /// <param name="newInstance">if set to <c>true</c> [new instance].</param>
        /// <returns></returns>
        public static ApplicationUnderTest Launch(bool newInstance)
        {
            ApplicationUnderTest ultiproWApplication = new ApplicationUnderTest();
            //Playback.Initialize();

            if (newInstance)
            {
                try
                {
                    ultiproWApplication = ApplicationUnderTest.Launch(Helpers.GetConfigValue("ApplicationPath"), Helpers.GetConfigValue("AlternateApplicationPath"));
                }
                catch (Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToLaunchApplicationException e)
                {
                }
                finally
                {
                    //Playback.Cleanup();
                }
            }
            else
            {
                Process proc = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Helpers.GetConfigValue("ApplicationPath")))[0];

                ultiproWApplication = ApplicationUnderTest.FromProcess(proc);
            }

            return ultiproWApplication;
        }

        /// <summary>
        /// Login
        /// </summary>
        /// <param name="companyCode"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="componentCompanyName"></param>
        public static void Login(string companyCode, string userName, string password, string componentCompanyName = "")
        {
            Keyboard.SendKeys(PageObjects.Login.LoginCompanyCode, companyCode);

            Keyboard.SendKeys(PageObjects.Login.LoginUserName, userName);

            Keyboard.SendKeys(PageObjects.Login.LoginPassword, password);

            // Click 'OK' button
            Mouse.Click(PageObjects.Login.OKButton);

            if (!string.IsNullOrEmpty(componentCompanyName))
            {
                SelectComponentCompany(componentCompanyName);
             
                //Mouse.Click(UIMap.UISelectacompanyWindow.UIItemWindow.CompanySelectorItemTree.UIMerrillsCalendarCompTreeItem.UIWebPayrollCompanyTreeItem);
            }

            Mouse.Click(PageObjects.Login.SelectButton);

            PageObjects.Login.ULTIPROWindow.WaitForControlEnabled(5000);
            PageObjects.Login.ULTIPROWindow.Maximized = true;
            PageObjects.Login.ULTIPROWindow.WaitForControlEnabled(2000);
        }

        /// <summary>
        /// Quits the ulti pro.
        /// </summary>
        public static void QuitUltiPro()
        {
            Process[] localByName = Process.GetProcessesByName("UltiproW");
            foreach (Process item in localByName)
            {
                item.Kill();
            }
        }

        /// <summary>
        /// Closes the current window.
        /// </summary>
        public static void CloseCurrentWindow()
        {
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIFileMenuItem);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UICloseButton);
        }
                
        /// <summary>
        /// Selects the component company.
        /// </summary>
        /// <param name="companyName">Name of the company.</param>
        public static void SelectComponentCompany(string companyName)
        {
            Mouse.Click(UIMap.UICompanyTreeItem(companyName));
        }

        /// <summary>
        /// Clicks the super finder.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickSuperFinder(string screen)
        {
            int xPos;
            int yPos;

            switch (screen)
            {
                case "PDE":
                    {
                        Mouse.Click(new Point(412, 86));
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(new Point(487, 86));
                    }
                    break;
            }
        }

        /// <summary>
        /// Find Employee by Empno.
        /// </summary>
        /// <param name="superFinder">The super finder.</param>
        /// <param name="empNo">The emp no.</param>
        public static void EmployeeFinderByEmpno(SuperFinder superFinder, string empNo)
        {
            superFinder.UISuperFinderTitleBar.WaitForControlEnabled();
            SetDropDown(superFinder.UIOrderByComboBox, "Emp No, Name");
            Mouse.Click(superFinder.FindEditBox);

            SetEditBox(superFinder.FindEditBox, empNo, true);

            Mouse.Click(superFinder.UIGOButton);
            superFinder.SuperFinderClientEmployeeList.WaitForControlEnabled();
            Mouse.Click(superFinder.UIOKButton);
        }

        /// <summary>
        /// Selects the context menu item.
        /// </summary>
        /// <param name="menuItemText">The menu item text.</param>
        public static void SelectContextMenuItem(string menuItemText)
        {
            Mouse.Click(UIMap.UIContextMenuItem(menuItemText));
        }

        /// <summary>
        /// Opens code setup.
        /// </summary>
        /// <param name="codeTable">The code table.</param>
        public static void OpenCodeSetup(string codeTable)
        {
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIFileMenuItem);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIFileMenuItem.UISetupMenuItem);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIFileMenuItem.UISetupMenuItem.UICodesMenuItem);

            SetDropDown(UIMap.UIULTIPROWindow.UICodeSetupWindow.UIItemWindow.CodeSetupSelector, codeTable);
            UIMap.UIULTIPROWindow.UICodeSetupWindow.WaitForControlEnabled(3000);
        }

        /// <summary>
        /// Opens pay data entry.
        /// </summary>
        public static void OpenPayDataEntry()
        {
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIPayrollMenuItem);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIPayrollMenuItem.UIPayDataEntryMenuItemPDE);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIPayrollMenuItem.UIPayDataEntryMenuItemPDE.UIStandardPayDataEntryMenuItem);

            UIMap.UIULTIPROWindow.UIStandardPayDataEntryWindow.WaitForControlEnabled(10000);
        }

        /// <summary>
        /// Opens view edit.
        /// </summary>
        public static void OpenViewEdit()
        {
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIEmployeeMenuItem);
            Mouse.Click(UIMap.UIULTIPROWindow.UIApplicationMenuBar.UIEmployeeMenuItem.UIViewEditMenuItem);
            UIMap.UIULTIPROWindow.UICodeSetupWindow.UIViewEdit.WaitForControlEnabled(10000);
        }

        /// <summary>
        /// Accepts the confirm window.
        /// </summary>
        public static void ConfirmWindowOK()
        {
            Mouse.Click(UltiProScreen.ConfirmWindow.ConfirmYesButton);
        }

        /// <summary>
        /// Cancels the confirm window.
        /// </summary>
        public static void ConfirmWindowCancel()
        {
            Mouse.Click(UltiProScreen.ConfirmWindow.ConfirmNOButton);
        }

        /// <summary>
        /// Clicks the add button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickAddButton(string screen)
        {
            switch (screen)
            {
                case "PDE":
                    {
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(Helpers.ScaledPosition(656, 89));
                    }
                    break;
                case "CodeSetup":
                    {
                        Mouse.Click(new Point(592, 89));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the delete button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickDeleteButton(string screen)
        {
            switch (screen)
            {
                case "PDE":
                    {
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(new Point(678, 89));
                    }
                    break;
                case "CodeSetup":
                    {
                        Mouse.Click(new Point(613, 89));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the save button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickSaveButton(string screen)
        {
            switch (screen)
            {
                case "PDE":
                    {
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(new Point(702, 89));
                    }
                    break;
                case "CodeSetup":
                    {
                        Mouse.Click(new Point(636, 89));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the undo button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickUndoButton(string screen)
        {
            switch (screen)
            {
                case "PDE":
                    {
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(new Point(723, 89));
                    }
                    break;
                case "CodeSetup":
                    {
                        Mouse.Click(new Point(657, 89));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the refresh button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickRefreshButton(string screen)
        {
            switch (screen)
            {
                case "PDE":
                    {
                    }
                    break;
                case "ViewEdit":
                    {
                        Mouse.Click(new Point(746, 89));
                    }
                    break;
                case "CodeSetup":
                    {
                        Mouse.Click(new Point(679, 89));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the save as button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickSaveAsButton(string screen)
        {
            switch (screen)
            {
                case "CodeSetup":
                    {
                        Mouse.Click(CodeSetup.TopPanel, Helpers.ScaledPosition(711, 13));
                    }
                    break;
            }
        }

        /// <summary>
        /// Clicks the company filter button.
        /// </summary>
        /// <param name="screen">The screen.</param>
        public static void ClickCompanyFilterButton(string screen)
        {
            switch (screen)
            {
                case "CodeSetup":
                    {
                        Mouse.Click(CodeSetup.TopPanel, Helpers.ScaledPosition(742, 13));
                    }
                    break;
            }
        }

        /// <summary>
        /// Sets the drop down.
        /// </summary>
        /// <param name="dropDown">The drop down.</param>
        /// <param name="dropdownOption">The dropdown option.</param>
        public static void SetDropDown(UITestControl dropDown, string dropdownOption)
        {
            Keyboard.SendKeys(dropDown, dropdownOption);
        }

        /// <summary>
        /// Sets the ComboBox.
        /// </summary>
        /// <param name="comboBox">The combo box.</param>
        /// <param name="dropdownOption">The dropdown option.</param>
        public static void SetComboBox(WinEdit comboBox, string dropdownOption)
        {
            Keyboard.SendKeys(comboBox, dropdownOption);

            while (!comboBox.Text.Equals(dropdownOption))
            {
                Keyboard.SendKeys(comboBox, "{DOWN}");
                Keyboard.SendKeys(comboBox, "{DOWN}");
            }

            Keyboard.SendKeys(comboBox, "{TAB}");
        }

        /// <summary>
        /// Sets the edit box.
        /// </summary>
        /// <param name="editBox">The edit box.</param>
        /// <param name="editValue">The edit value.</param>
        /// <param name="clearField">if set to <c>true</c> [clear field].</param>
        public static void SetEditBox(WinEdit editBox, string editValue, bool clearField)
        {
            editBox.WaitForControlExist(5000);

            if (clearField)
            {                
                Keyboard.SendKeys(editBox, "{HOME}", ModifierKeys.Shift);
                Keyboard.SendKeys("{DELETE}");
            }
            
            Keyboard.SendKeys(editBox, editValue);
            Keyboard.SendKeys(editBox, "{TAB}");
        }     
    }
}

/*
 *  Codes for KeyBoard.PressKeys
Key	Code
Backspace	{BACKSPACE}, {BS} or {BKSP}
Pause	{BREAK}
Capslock	{CAPSLOCK}
Delete	{DELETE} or {DEL}
End	{END}
Return	{ENTER} or ~
Esc	{ESC}
Help	{HELP}
Pos1	{HOME}
Insert	{INSERT} or {INS}
Numlock	{NUMLOCK}
Page Down	{PGDN}
Page Up	{PGUP}
Print	{PRTSC}
Scrollock	{SCROLLLOCK}
Tab	{TAB}
F1…	{F1}
Cursor up	{UP}
Cursor down	{DOWN}
Cursor left	{LEFT}
Cursor right	{RIGHT}
Addition (num block)	{ADD}
Subtraction (num block)	{SUBTRACT}
Multiplication (num block)	{MULTIPLY}
Division (num block))	{DIVIDE}
*/